$(document).ready(function () {
    const apiUrl = "http://127.0.0.1:8080/queryTeacher";
    const pageSize = 10; // 每页显示的条数
    let currentPage = 1; // 当前页码

    // 加载教师数据
    function loadTeachers(params = {}) {
        params.page = currentPage; // 添加分页参数
        params.limit = pageSize;

        $.ajax({
            url: apiUrl,
            method: "GET",
            data: params,
            success: function (res) {
                renderTable(res);
                renderPagination(res.length); // 假设返回的数组中包含当前页数据
            },
            error: function () {
                alert("加载数据失败，请检查服务端是否启动。");
            }
        });
    }

    // 渲染表格
    function renderTable(data) {
        const tbody = $("#teacherTableBody");
        tbody.empty(); // 清空表格内容

        if (data.length === 0) {
            tbody.append("<tr><td colspan='9'>没有数据</td></tr>");
            return;
        }

        data.forEach(item => {
            const row = `
                <tr>
                    <td>${item.teacherid}</td>
                    <td>${item.name}</td>
                    <td>${item.gender === "F" ? "女" : "男"}</td>
                    <td>${item.phone}</td>
                    <td>${item.address}</td>
                    <td>${item.partymember ? "是" : "否"}</td>
                    <td>${item.position}</td>
                    <td>${item.departmentid}</td>
                    <td>
                        <a href="edit_teacher.html?id=${item.teacherid}" class="button">修改</a>
                        <button class="delete-btn" onclick="deleteTeacher(${item.teacherid})">删除</button>
                    </td>
                </tr>`;
            tbody.append(row);
        });
    }

    // 渲染分页
    function renderPagination(totalCount) {
        const totalPages = Math.ceil(totalCount / pageSize);
        const pagination = $(".pagination");

        $("#totalPages").text(totalPages);
        $("#currentPage").text(currentPage);

        pagination.find(".page-button").removeClass("disabled");

        if (currentPage === 1) {
            $("#firstPageBtn, #prevPageBtn").addClass("disabled");
        }
        if (currentPage === totalPages) {
            $("#nextPageBtn, #lastPageBtn").addClass("disabled");
        }
    }

    // 改变页面
    window.changePage = function (delta) {
        currentPage += delta;
        loadTeachers();
    };

    window.goToPage = function (page) {
        currentPage = page;
        loadTeachers();
    };

    // 搜索教师
    window.searchTeacher = function () {
        const teacherId = $("#teacherId").val();
        const teacherName = $("#teacherName").val();
        const teacherPhone = $("#teacherPhone").val();

        loadTeachers({
            teacherId,
            teacherName,
            teacherPhone
        });
    };

    // 删除教师
    window.deleteTeacher = function (teacherId) {
        if (confirm("确认删除该教师信息吗？")) {
            $.ajax({
                url: `${apiUrl}/delete`, // 假设后端支持 DELETE 请求
                method: "POST", // 如果后端不支持 DELETE，用 POST 模拟
                data: { teacherId },
                success: function (res) {
                    if (res.success) {
                        alert("删除成功！");
                        loadTeachers();
                    } else {
                        alert("删除失败！");
                    }
                },
                error: function () {
                    alert("删除操作失败！");
                }
            });
        }
    };

    // 初始化加载数据
    loadTeachers();
});
